package com.cms.dao;

import java.util.List;

import org.springframework.stereotype.Component;

import com.cms.base.HibernateDAOImpl;
import com.cms.base.Pagination;
import com.cms.entity.Module;

@Component(value="moduleDao")
public class ModuleDao extends HibernateDAOImpl<Module> {
	
	public List<Module> queryModules(Module module, int pid){
		String hql = "";
		if(pid == 0)
			hql += "select m from Module m where m.parent is null ";
		else
			hql += "select m from Module m where m.parent.id = "+pid;
		//if
		List<Module> list = this.getSession().createQuery(hql).list();
		return list;
	}
	
	
	public Pagination<Module> queryAllModules(Module module, int firstResult, int maxResults){
		String hql = "select m from Module m where 1=1 ";
		List<Module> list = this.getSession()
				.createQuery(hql)
				.setFirstResult(firstResult)
				.setMaxResults(maxResults)
				.list();
		
		String countHql = "select count(m.id) from Module m where 1=1 ";
		int total = ((Long)(this.getSession().createQuery(countHql).uniqueResult())).intValue();
		
		Pagination<Module> pagination = new Pagination<Module>();
		pagination.setData(list);
		pagination.setTotal(total);
		return pagination;
	}
	
	public List<Module> queryModuleTree(){
		String hql = "select m from Module m ";
		List<Module> list = this.getSession()
				.createQuery(hql)
				.list();
		return list;
	}
	
}
